import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  Request,
  UseFilters,
  UseGuards,
} from '@nestjs/common';
import { ApplicationService } from './application.service';
import { AddApplicationDto } from './dto/add-application.dto';
import { AddApplicationResponseDto } from './dto/addApplication-response.dto';
import { AuthGuard } from '@nestjs/passport';
import { HttpExceptionFilter } from '../../common/filters/http-exception.filter';
import { UpdateResult } from 'typeorm';
import type { RequestWithUser } from './interfaces/request-with-user.interface';

@Controller('application')
export class ApplicationController {
  constructor(private readonly applicationService: ApplicationService) {}

  // 新增应用
  @Post('add')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  // 当我们将 AddApplicationDto 用作类型时，它表示的是一个对象，这个对象具有与 AddApplicationDto 类实例相同的结构
  async addApplication(@Request() req: RequestWithUser, @Body() addApplicationDto: AddApplicationDto) {
    // 从JWT中获取用户ID
    // 将用户ID添加到DTO中
    addApplicationDto.user_id = req.user.uid;
    const addApplication = await this.applicationService.addApplication(addApplicationDto);

    return new AddApplicationResponseDto(2000, '新增成功', addApplication);
  }
  // 查询收藏应用
  @Get('collect')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async getCollectApplication(@Request() req: RequestWithUser) {
    // 从JWT中获取用户ID
    const user_id = req.user.uid;
    const collectApplications = await this.applicationService.findCollectApplications(user_id);
    return new AddApplicationResponseDto(2000, '查询成功', collectApplications);
  }
  // 查询应用
  @Get('query')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async getQueryApplication(
    @Request() req: RequestWithUser,
    @Query('aid') aid: number,
    @Query('keyword') keyword?: string,
  ) {
    // 从JWT中获取用户ID
    const user_id = req.user.uid;
    const queryApplications = await this.applicationService.findQueryApplications(user_id, keyword, aid);
    return new AddApplicationResponseDto(2000, '查询成功', queryApplications);
  }
  // 修改应用名称、图标和背景色
  @Patch('update/:aid')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async updateApplicationName(
    @Param('aid') aid: number,
    @Body('name') name?: string,
    @Body('icon') icon?: string,
    @Body('bg_color') bgColor?: string[],
    @Body('bg_color_type') bg_color_type?: string,
  ) {
    const updateApplication = await this.applicationService.updateApplicationName(
      aid,
      name,
      icon,
      bgColor,
      bg_color_type,
    );
    return new AddApplicationResponseDto(2000, '修改成功', updateApplication);
  }
  // 删除应用
  @Delete('delete/:aid')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async deleteApplication(@Param('aid') aid: number) {
    const deleteApplication = await this.applicationService.deleteApplication(aid);
    return new AddApplicationResponseDto(2000, '删除成功', deleteApplication);
  }
  // 收藏应用
  @Post('collect/:aid')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async collectApplication(@Param('aid') aid: number) {
    const collectApplication = await this.applicationService.collectApplication(aid);
    return new AddApplicationResponseDto<UpdateResult>(2000, '收藏成功', collectApplication);
  }
  // 取消收藏应用
  @Post('cancel-collect/:aid')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async cancelCollectApplication(@Param('aid') aid: number) {
    const cancelCollectApplication = await this.applicationService.cancelCollectApplication(aid);
    return new AddApplicationResponseDto<UpdateResult>(2000, '取消收藏成功', cancelCollectApplication);
  }
  // 批量收藏应用
  @Post('batch-collect')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async batchCollectApplications(@Body('aids') aids: number[]) {
    const collectApplications = await this.applicationService.batchCollectApplications(aids);
    return new AddApplicationResponseDto<UpdateResult>(2000, '批量收藏成功', collectApplications);
  }
  // 批量取消收藏应用
  @Post('batch-cancel-collect')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async batchCancelCollectApplications(@Body('aids') aids: number[]) {
    const cancelCollectApplications = await this.applicationService.batchCancelCollectApplications(aids);
    return new AddApplicationResponseDto<UpdateResult>(2000, '批量取消收藏成功', cancelCollectApplications);
  }
  // 增加应用点击次数
  @Post('click/:aid')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async incrementClickCount(@Param('aid') aid: number) {
    const result = await this.applicationService.incrementClickCount(aid);
    return new AddApplicationResponseDto<UpdateResult>(2000, '增加点击次数成功', result);
  }
  // 最近使用应用
  @Get('recent')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async getRecentApplication(@Request() req: RequestWithUser) {
    // 从JWT中获取用户ID
    const user_id = req.user.uid;
    const recentApplications = await this.applicationService.findRecentApplications(user_id);
    return new AddApplicationResponseDto(2000, '查询成功', recentApplications);
  }
  // 更新应用图片
  @Patch('update-image/:aid')
  @UseGuards(AuthGuard('auth'))
  @UseFilters(HttpExceptionFilter)
  async updateApplicationImage(@Param('aid') aid: number, @Body('image_url') image_url: string) {
    const updateApplication = await this.applicationService.updateApplicationImage(aid, image_url);
    return new AddApplicationResponseDto<UpdateResult>(2000, '更新成功', updateApplication);
  }
}
